import { post, BaseRes } from "../libs/request";
import { Identity, Relevance } from "../models/enums";
import { IBooks, IUser, Remark } from "../models/types";
// API路径前缀
const PREFIX = "/api/admin";

export interface Book {
  _id: string;
  banner: string[];
  title: string;
  lowprice: number;
  allstock: number;
  isDelete: boolean;
  putaway: boolean;
}

interface Users extends BaseRes {
  user: IUser;
}

interface Books extends BaseRes {
  data: {
    list: Book[];
    total: number;
  };
}
interface Users extends BaseRes {
  data: {
    list: IUser[];
    total: number;
  };
}

export interface Detail extends BaseRes {
  data: {
    book: IBooks;
    user: IUser;
  };
}

interface Remarks extends BaseRes {
  data: {
    list: Remark[];
    total: number;
  };
}

export async function login(username: string, password: string) {
  const { data } = await post<BaseRes>(PREFIX + "/login", {
    username,
    password,
  });
  return data;
}

export async function judges(skip: number, limit: number) {
  const { data } = await post<Books>(PREFIX + "/judge/list", {
    skip,
    limit,
  });
  return data;
}

export async function booklist(skip: number, limit: number, search?: string) {
  const { data } = await post<Books>(PREFIX + "/books", {
    skip,
    limit,
    search,
  });
  return data;
}

export async function detail(bookId: string, token?: string) {
  const { data } = await post<Detail>(
    PREFIX + "/book/detail",
    {
      bookId,
    },
    token
  );
  return data;
}

export async function userlist(skip: number, limit: number, search?: string) {
  const { data } = await post<Users>(PREFIX + "/users", {
    skip,
    limit,
    search,
  });
  return data;
}

export async function remarklist(skip: number, limit: number) {
  const { data } = await post<Remarks>(PREFIX + "/remarks", {
    skip,
    limit,
  });
  return data;
}

export async function judgebook(
  bookId: string,
  putaway: boolean,
  content: string
) {
  const { data } = await post<BaseRes>(PREFIX + "/judge", {
    bookId,
    putaway,
    content,
  });
  return data;
}

export async function dealremark(remarkId: string, flag: boolean) {
  const { data } = await post<BaseRes>(PREFIX + "/deal/remark", {
    remarkId,
    flag,
  });
  return data;
}

export async function dealuser(userId: string, role: Identity) {
  const { data } = await post<BaseRes>(PREFIX + "/deal/user", {
    userId,
    role,
  });
  return data;
}
